package com.noahplat.common.utils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.Map;

/**
 * @ClassName: JpushUtils
 * @Description: 极光推送工具类
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 * map存储示例：
 * //建立参数集合
 * Map<String, String> parm = new HashMap<>();
 * //设置标题
 * parm.put("title","Alin");
 * //设置内容
 * parm.put("msg","Hello Alin !");
 * //设置registionID
 * parm.put("id", "13065ffa4ea4683802f");
 */
public class JpushUtils {

    // 设置app_key和masterSecret
    private static String APP_KEY = "03cbc380d83c752d70ec9c8b";
    private static String MASTER_SECRET = "82764e8f7d2dc8443c802e6d";

    /**
     * android端消息单点推送
     *
     * @param parm
     */
    public static void jpushAndroid(Map<String, String> parm) {
        //建立JPushClient(极光推送的实例)
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台
//                .setAudience(Audience.all())//通知所有  即广播（免费版一天只有10次机会）
                .setAudience(Audience.registrationId(parm.get("id")))//指定用户发送
                .setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(7200).build())
                // TimeToLive 缓存时间
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))  //发送通知
                .build();//建立
        try {
            PushResult pushResult = jpushClient.sendPush(payload);
            System.out.println(pushResult.toString());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    /**
     * ios端消息单点推送
     *
     * @param parm
     */
    public static void jpushIOS(Map<String, String> parm) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())//ios平台
                //.setAudience(Audience.all())//全部用户
                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(parm.get("msg"))
                                .setBadge(+1)
                                .setSound("happy")//设置提示音
                                .addExtras(parm)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                .setMessage(cn.jpush.api.push.model.Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
                .build();
        try {
            PushResult pushResult = jpushClient.sendPush(payload);
            System.out.println(pushResult.toString());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    /**
     * android和ios端同时推送
     *
     * @param parm
     */
    public static void jpushAll(Map<String, String> parm) {
        //建立JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //建立option
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())  //全部平台（安卓+ios）
                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder() //发送ios
                                .setAlert(parm.get("msg")) //消息体
                                .setBadge(+1)
                                .setSound("happy") //ios提示音
                                .addExtras(parm) //附加参数
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                .addExtras(parm) //附加参数
                                .setAlert(parm.get("msg")) //消息体
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setMessage(cn.jpush.api.push.model.Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
                .build();
        try {
            PushResult pushResult = jpushClient.sendPush(payload);
            System.out.println("pushResult = " + pushResult.toString());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
}
